
using System;
using System.Net;

namespace MxitAPI
{
	
	/**
	 * Server address information for an Http connection to one of the Mxit servers ( Possibly through a proxy )
	 * Also keeps track of the sequence number and session ID
	 */
	public class HttpServerInfo : ServerInfo {
		
		private static log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
		
		private int sequenceNumber;
		public int SessionID { get; set; }  /// Session ID returned by Mxit after login. Is null if the user has not logged in
		public WebProxy Proxy { get; set; }
		
		public HttpServerInfo() {
			Proxy = GetProxySettings();
		}
		
		public int SequenceNumber {
			get { return sequenceNumber; }
		}
		
		public void IncrementSequenceNumber() {
			sequenceNumber++;	
		}
		
		protected WebProxy GetProxySettings() {
			
			string proxyAddress = System.Configuration.ConfigurationManager.AppSettings["ProxyAddress"];
			string proxyUsername = System.Configuration.ConfigurationManager.AppSettings["ProxyUsername"];
			string proxyPassword = System.Configuration.ConfigurationManager.AppSettings["ProxyPassword"];
			
			WebProxy proxy = null;
			
			if ( proxyAddress != null) {
				
				log.Info( "Proxy specified" );
				
				proxy = new WebProxy( proxyAddress, true );
				
				if ( proxyUsername != null ) log.InfoFormat( "Proxy Username: [{0}]", proxyUsername );
				if ( proxyPassword != null ) log.InfoFormat( "Proxy Password given" );
				
				proxy.Credentials = new NetworkCredential( proxyUsername, proxyPassword );
			}
			else {
				log.Info( "No proxy specified" );
			}
			
			return proxy;			
		}


	}
}
